package com.taiyi.renaissancemall.ui.guidepage

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.View.*
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.ScreenUtils
import com.taiyi.renaissancemall.Utils.Utils
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.base.normalvp.NormalPresenter
import com.taiyi.renaissancemall.base.normalvp.NormalView
import com.taiyi.renaissancemall.ui.MainActivity
import kotlinx.android.synthetic.main.activity_guide_page.*

class GuidePageActivity : BaseActivity<NormalView, NormalPresenter>() {
    var imgList = mutableListOf<ImageView>()


    inner class MyPagerAdapter : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`


        override fun getCount(): Int = imgList.size

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
            container.removeView(`object` as View?)
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            var iv = imgList[position]
            iv.scaleType = ImageView.ScaleType.FIT_XY
//            var v = LayoutInflater.from(this@GuidePageActivity)
//                .inflate(R.layout.launch_image_layout, null)
//            var iv = v.findViewById<ImageView>(R.id.iv)
//            iv.setImageResource(imgList[position])
            container.addView(iv)
            return iv
        }

    }

    override fun getLayoutId(): Int = R.layout.activity_guide_page

    override fun initPresenter(): NormalPresenter = NormalPresenter()

    override fun initViews(savedInstanceState: Bundle?) {

        var isFirstLaunch = Utils.getSpUtils().getInt("isFirstLaunch", 0)
        if (isFirstLaunch != 0) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
        if (ScreenUtils.isAllScreenDevice(this)) {//全屏

            if (ScreenUtils.checkDeviceHasNavigationBar(this) && ScreenUtils.hasNavigationBar(this)) {//有虚拟键盘、显示虚拟键盘

//                iv_bg.setImageResource(R.mipmap.iv_welcome_bg_1080_2106);
                var img1 = ImageView(this)
                img1.setImageResource(R.mipmap.one_2106_iv)
                var img2 = ImageView(this)
                img2.setImageResource(R.mipmap.two_2106_iv)
                var img3 = ImageView(this)
                img3.setImageResource(R.mipmap.three_2106_iv)
                imgList.run {
                    add(img1)
                    add(img2)
                    add(img3)
                }

            } else {//无虚拟键盘、没有显示虚拟键盘

//                iv_bg.setImageResource(R.mipmap.iv_welcome_bg_1080_2340);
                var img1 = ImageView(this)
                img1.setImageResource(R.mipmap.one_2340_iv)
                var img2 = ImageView(this)
                img2.setImageResource(R.mipmap.two_2340_iv)
                var img3 = ImageView(this)
                img3.setImageResource(R.mipmap.three_2340_iv)
                imgList.run {
                    add(img1)
                    add(img2)
                    add(img3)
                }
            }

        } else {//不是全屏

//            iv_bg.setImageResource(R.mipmap.iv_welcome_bg_720_1280);
            var img1 = ImageView(this)
            img1.setImageResource(R.mipmap.one_1080_iv)

            var img2 = ImageView(this)
            img2.setImageResource(R.mipmap.two_1080_iv)
            var img3 = ImageView(this)
            img3.setImageResource(R.mipmap.three_1080_iv)
            imgList.run {
                add(img1)
                add(img2)
                add(img3)
            }
        }


        viewPager.adapter = MyPagerAdapter()

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                Log.i("sssssss", "----$position")
                if (position == 2) {
                    tv.visibility = VISIBLE
                    iv1.visibility = GONE
                    iv2.visibility = GONE
                    iv3.visibility = GONE
                } else if (position == 1) {
                    tv.visibility = INVISIBLE
                    iv1.visibility = VISIBLE
                    iv2.visibility = VISIBLE
                    iv3.visibility = VISIBLE
                    iv1.setImageResource(R.mipmap.iv_no)
                    iv2.setImageResource(R.mipmap.iv_yes)
                    iv3.setImageResource(R.mipmap.iv_no)
                } else {
                    tv.visibility = INVISIBLE
                    iv1.visibility = VISIBLE
                    iv2.visibility = VISIBLE
                    iv3.visibility = VISIBLE
                    iv1.setImageResource(R.mipmap.iv_yes)
                    iv2.setImageResource(R.mipmap.iv_no)
                    iv3.setImageResource(R.mipmap.iv_no)
                }
            }

        })
    }

    override fun initData() {

    }

    override fun initEvent() {
        tv.setOnClickListener {

            Utils.getSpUtils().putInt("isFirstLaunch", 1)
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }
}


